<template>
  <div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="60%">
      <TreeGoods @sendTreeData="sendTreeData" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="innerVisible = true"
          :disabled="isDisabled"
        >
          确定并添加分组</el-button
        >
      </span>
      <!-- 二级嵌套 -->
      <el-dialog
        width="45%"
        title="商品规格参数配置"
        :visible.sync="innerVisible"
        append-to-body
      >
        <div class="title">当前选中的商品:{{ treeData.name }}</div>
        <el-button class="add" type="primary" @click="addDomain"
          >新增规格列表</el-button
        >

        <hr />

        <!-- 动态添加删除 -->
        <el-form
          :model="dynamicValidateForm"
          ref="dynamicValidateForm"
          label-width="120px"
          class="demo-dynamic"
        >
          <el-form-item
            v-for="(item, index) in dynamicValidateForm.groups"
            :label="item.title"
            :key="index"
            :prop="item.title"
            :rules="{
              required: true,
              message: '域名不能为空',
              trigger: 'blur',
            }"
          >
            <div class="item">
              <el-input v-model="item.title"></el-input>
              <el-button @click.prevent="addChildDomain(index)"
                >增加子组</el-button
              >
              <el-button @click.prevent="removeDomain(index)">删除</el-button>
            </div>

            <el-form-item
              v-for="(ele, i) in item.chiledern"
              :label="ele.title"
              :key="i"
              :prop="ele.title"
              :rules="{
                required: true,
                message: '域名不能为空',
                trigger: 'blur',
              }"
            >
              <div class="item">
                <el-input v-model="ele.title"></el-input>
                <el-button @click.prevent="removeChildDomain(index, i)"
                  >删除</el-button
                >
              </div>
            </el-form-item>
          </el-form-item>
          <!-- <el-form-item>
            <el-button type="primary" @click="submitForm('dynamicValidateForm')"
              >提交</el-button
            >

            <el-button @click="resetForm('dynamicValidateForm')"
              >重置</el-button
            >
          </el-form-item> -->
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm('dynamicValidateForm')"
            >确定</el-button
          >
          <el-button @click="cancel">取消</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import TreeGoods from "@/views/Goods/TreeGoods.vue";

export default {
  data() {
    return {
      dialogVisible: false,
      innerVisible: false,
      isDisabled: true,
      treeData: {},
      dynamicValidateForm: {
        groups: [],
      },
    };
  },
  components: { TreeGoods },
  methods: {
    // 删除子组
    removeChildDomain(index, i) {
      this.dynamicValidateForm.groups[index].chiledern.splice(i, 1);
    },

    // addChildDomain
    addChildDomain(index) {
      this.dynamicValidateForm.groups[index].chiledern.push({
        value: "",
        title: "",
      });
    },

    sendTreeData(val) {
      console.log(val);
      this.treeData = val;
      this.isDisabled = false;
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // alert("submit!");
          console.log(this.dynamicValidateForm.groups);
          this.$api
            .insertItemParam({
              itemCatId: this.treeData.cid,
              content: JSON.stringify(this.dynamicValidateForm.groups),
              specsName: this.treeData.name,
            })
            .then(res => {
              console.log(res.data);
              if (res.data.status === 200) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.dialogVisible = false;
                this.innerVisible = false;
                this.$parent.http(1);
                this.dynamicValidateForm.groups = [];
                this.isDisabled = true;
                // this.$emit("sendParamsData", res.data.data);
              } else {
                this.$message({
                  message: "添加失败",
                  type: "error",
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");

          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    removeDomain(index) {
      if (index !== -1) {
        this.dynamicValidateForm.groups.splice(index, 1);
      }
    },
    addDomain() {
      this.dynamicValidateForm.groups.push({
        title: "",
        value: "",
        chiledern: [],
      });
    },
    cancel(){
      this.dialogVisible = false;
      this.innerVisible = false;
      this.dynamicValidateForm.groups = [];
      this.isDisabled = true;
    }
  },
};
</script>

<style lang="scss" scoped>
.add {
  margin: 10px;
}
.item {
  display: flex;
  margin-bottom: 10px;
  button {
    margin-left: 10px;
  }
}
</style>
